package com.example.AoUserApp;

import com.example.AoUserApp.utils.RSAUtils;
import com.fazecast.jSerialComm.SerialPort;
import javafx.collections.FXCollections;
import javafx.event.Event;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import java.io.*;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;

public class HelloApplication extends Application {

    static Timer timer1 = null;
    static TimerTask timerTask1 = null;
    static Long text = null;
    static SerialPort[] serialPorts = SerialPort.getCommPorts();
    static SerialPort port = null;
    static CopyOnWriteArrayList<String> resultList = new CopyOnWriteArrayList<>();
    static Timer timer2 = null;
    static TimerTask timerTask2 = null;
    static Map<String, String> configurationMap = new HashMap<>();
    static Stage stage1 = null;
    static Stage stage2 = null;
    static Stage stage3 = null;
    static Stage newstage = null;
    static Stage stage4 = null;
    static Long Max_time = null;
    static Timer timer = null;

    @Override
    public void start(Stage stage) throws IOException {
        // 加载主窗体
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load());
        // 设置窗体标题
        stage.setTitle("AIOPTICS-iNFA User App");
        stage.setScene(scene);
        // 加载窗体内的组件
        Pane lookup = (Pane) scene.lookup("#headPane");
        Pane allpane = (Pane) scene.lookup("#allPane");
        Pane pane2 = (Pane) scene.lookup("#pane2");
        Pane pane3 = (Pane) scene.lookup("#pane3");
        Pane pane4 = null;

        ImageView imglabel = (ImageView) scene.lookup("#imgAA");
        Label titleLB = (Label) scene.lookup("#titleLB");
        Label connectLB = (Label) scene.lookup("#connectLB");
        Button rsBT = (Button) scene.lookup("#rs485BT");
        Button etBT = (Button) scene.lookup("#ethernetBT");
        Button usbBT = (Button) scene.lookup("#usbBT");

        Label deLB = (Label) scene.lookup("#deLB");
        Label MOLB = (Label) scene.lookup("#MOLB");
        Label PNLB = (Label) scene.lookup("#PNLB");
        Label SNLB = (Label) scene.lookup("#SNLB");
        Label FWLB = (Label) scene.lookup("#FWLB");
        Label HWLB = (Label) scene.lookup("#HWLB");
        Label BLLB = (Label) scene.lookup("#BLLB");
        Button ENGBT = (Button) scene.lookup("#ENGBT");
        Label ENGXLB = (Label) scene.lookup("#ENGXLB");
        Label STLB = (Label) scene.lookup("#STLB");
        Label OKLB = (Label) scene.lookup("#OKLB");

        Label cpLB = (Label) scene.lookup("#cpLB");
        Label SPrLB = (Label) scene.lookup("#SPrLB");
        Label SPRRLB = (Label) scene.lookup("#SPRRLB");
        Label SPeLB = (Label) scene.lookup("#SPeLB");
        Label powerLBW = (Label) scene.lookup("#powerLBW");
        Label powerLBmW = (Label) scene.lookup("#powerLBmW");
        Label powerLBdBm = (Label) scene.lookup("#powerLBdBm");
        Label prrLBHz = (Label) scene.lookup("#prrLBHz");
        Label prrLBKHz = (Label) scene.lookup("#prrLBKHz");
        Label prrLBMHz = (Label) scene.lookup("#prrLBMHz");
        Label slLB = (Label) scene.lookup("#slLB");
        Label pulseFsLB = (Label) scene.lookup("#pulseFsLB");
        Label pulsePsLB = (Label) scene.lookup("#pulsePsLB");

        TextField powerTF = (TextField) scene.lookup("#powerTF");
        TextField prrTF = (TextField) scene.lookup("#prrTF");
        TextField pulseTF = (TextField) scene.lookup("#pulseTF");

        HBox sprHB = (HBox) scene.lookup("#sprHB");
        HBox sprrHB = (HBox) scene.lookup("#sprrHB");
        HBox speHB = (HBox) scene.lookup("#speHB");
        HBox sgHB = (HBox) scene.lookup("#sgHB");

        Button set1 = (Button) scene.lookup("#set1");
        Button set2 = (Button) scene.lookup("#set2");
        Button set3 = (Button) scene.lookup("#set3");

        Slider powerSd = (Slider) scene.lookup("#powerSd");
        Slider pulseSd = (Slider) scene.lookup("#pulseSd");

        Button SAVEBT = (Button) scene.lookup("#SAVEBT");
        Button LOADBT = (Button) scene.lookup("#LOADBT");
        Button stopBT = (Button) scene.lookup("#stopBT");


        Label OPPLB = (Label) scene.lookup("#OPPLB");
        Label PRRLB = (Label) scene.lookup("#PRRLB");
        Label LB1 = (Label) scene.lookup("#LB1");
        Label LB2 = (Label) scene.lookup("#LB2");
        Label LogLB = (Label) scene.lookup("#LogLB");

        Accordion DBAD = (Accordion) scene.lookup("#DBAD");
        ObservableList<TitledPane> panes = DBAD.getPanes();
        for (TitledPane pane : panes) {
            pane.setPrefHeight(DBAD.getPrefHeight() - 9);
            pane4 = (Pane) pane.getContent();
        }
        Label DBLB = (Label) pane4.lookup("#DBLB");
        Label ECLB = (Label) pane4.lookup("#ECLB");
        Label APLB = (Label) scene.lookup("#APLB");

        VBox DBBox1 = (VBox) pane4.lookup("#DBBox1");
        VBox DBBox2 = (VBox) pane4.lookup("#DBBox2");
        VBox DBBox3 = (VBox) pane4.lookup("#DBBox3");
        VBox DBBox4 = (VBox) pane4.lookup("#DBBox4");

        Label RELB = (Label) pane4.lookup("#RELB");
        Label SingleLB = (Label) pane4.lookup("#SingleLB");
        Label AUTOLB = (Label) pane4.lookup("#AUTOLB");
        TextField NumLB = (TextField) pane4.lookup("#NumLB");
        Label SULB = (Label) pane4.lookup("#SULB");

        Label PD1 = (Label) pane4.lookup("#PD1");
        Label PD2 = (Label) pane4.lookup("#PD2");
        Label PD3 = (Label) pane4.lookup("#PD3");
        Label PD4 = (Label) pane4.lookup("#PD4");
        Label PD5 = (Label) pane4.lookup("#PD5");
        Label PD6 = (Label) pane4.lookup("#PD6");
        ListView TMPLV = (ListView) pane4.lookup("#TMPLV");
        Label LD1 = (Label) pane4.lookup("#LD1");
        Label LD2 = (Label) pane4.lookup("#LD2");
        Label HP1 = (Label) pane4.lookup("#HP1");
        Label HP2 = (Label) pane4.lookup("#HP2");
        Label HP3 = (Label) pane4.lookup("#HP3");
        Label HP4 = (Label) pane4.lookup("#HP4");
        Label FAOM = (Label) pane4.lookup("#FAOM");
        Label SAOM = (Label) pane4.lookup("#SAOM");
        Label MOTOR = (Label) pane4.lookup("#MOTOR");
        Label nu1 = (Label) pane4.lookup("#nu1");
        Label nu2 = (Label) pane4.lookup("#nu2");
        Label nu3 = (Label) pane4.lookup("#nu3");
        ListView PHLV = ((ListView) pane4.lookup("#PHLV"));

        MenuBar EGMB = (MenuBar) scene.lookup("#EGMB");


        // 总窗口宽度监听
        Pane finalPane = pane4;
        stage.widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observableValue, Number oldnumber, Number newnumber) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        double newnum = (double) newnumber;
                        lookup.setPrefWidth(newnum);
                        allpane.setPrefWidth(newnum);
                        pane2.setPrefWidth(newnum);
                        pane3.setPrefWidth(newnum);
                        DBAD.setPrefWidth(newnum);
                        finalPane.setPrefWidth(newnum);


                        imglabel.setFitWidth(newnum / 16.07);
                        imglabel.setLayoutX(newnum * 0.03);
                        titleLB.setPrefWidth(newnum / 3.27);
                        titleLB.setLayoutX(imglabel.getLayoutX() + imglabel.getFitWidth() * 1.5);
                        connectLB.setLayoutX(newnum * 0.8);

                        usbBT.setPrefWidth(newnum * 0.07);
                        usbBT.setLayoutX(connectLB.getLayoutX() - usbBT.getPrefWidth());
                        rsBT.setPrefWidth(newnum * 0.07);
                        rsBT.setLayoutX(usbBT.getLayoutX() + usbBT.getPrefWidth() * 1.2);
                        etBT.setPrefWidth(newnum * 0.07);
                        etBT.setLayoutX(rsBT.getLayoutX() + rsBT.getPrefWidth() * 1.2);

                        deLB.setPrefWidth(newnum * 0.485);
                        double prefWidth = deLB.getPrefWidth();
                        ENGBT.setPrefWidth(prefWidth * 0.375);
                        ENGBT.setLayoutX(prefWidth + 1);
                        ENGXLB.setPrefWidth(prefWidth * 0.375);
                        ENGXLB.setLayoutX(prefWidth + 1);

                        MOLB.setPrefWidth(prefWidth * 0.5);
                        PNLB.setPrefWidth(prefWidth * 0.5);
                        SNLB.setPrefWidth(prefWidth * 0.5);

                        FWLB.setPrefWidth(prefWidth * 0.5);
                        FWLB.setLayoutX(MOLB.getPrefWidth());
                        HWLB.setPrefWidth(prefWidth * 0.5);
                        HWLB.setLayoutX(PNLB.getPrefWidth());
                        BLLB.setPrefWidth(prefWidth * 0.5);
                        BLLB.setLayoutX(SNLB.getPrefWidth());

                        STLB.setPrefWidth(ENGBT.getPrefWidth() * 0.47);
                        STLB.setLayoutX(ENGBT.getLayoutX() + ENGBT.getPrefWidth() + 1);
                        OKLB.setPrefWidth(deLB.getPrefWidth() * 0.5);
                        OKLB.setLayoutX(STLB.getLayoutX() + STLB.getPrefWidth() + 1);

                        cpLB.setPrefWidth(newnum);
                        SPrLB.setPrefWidth(newnum * 0.07);
                        SPrLB.setLayoutX(newnum / 70);
                        powerLBW.setPrefWidth(newnum * 0.015);
                        powerLBmW.setPrefWidth(newnum * 0.025);
                        powerLBdBm.setPrefWidth(newnum * 0.03);
                        prrLBHz.setPrefWidth(powerLBW.getPrefWidth());
                        prrLBKHz.setPrefWidth(powerLBmW.getPrefWidth());
                        prrLBMHz.setPrefWidth(powerLBdBm.getPrefWidth());
                        powerTF.setPrefWidth(newnum / 6.125);
                        powerTF.setLayoutX(SPrLB.getLayoutX() + SPrLB.getPrefWidth());
                        powerSd.setLayoutX(SPrLB.getLayoutX());
                        powerSd.setPrefWidth(newnum * 0.435);
                        sprHB.setPrefWidth(SPrLB.getPrefWidth() * 1.2);
                        sprHB.setLayoutX(powerTF.getLayoutX() + powerTF.getPrefWidth() * 1.08);
                        set1.setPrefWidth(SPrLB.getPrefWidth() * 0.79);
                        set1.setLayoutX(sprHB.getLayoutX() + sprHB.getPrefWidth() * 1.5);

                        SPRRLB.setPrefWidth(newnum * 0.07);
                        SPRRLB.setLayoutX(newnum / 70);
                        slLB.setPrefWidth(powerSd.getPrefWidth());
                        slLB.setLayoutX(SPrLB.getLayoutX());
                        prrTF.setPrefWidth(newnum / 6.125);
                        prrTF.setLayoutX(SPRRLB.getLayoutX() + SPRRLB.getPrefWidth());
                        sprrHB.setPrefWidth(SPrLB.getPrefWidth() * 1.2);
                        sprrHB.setLayoutX(prrTF.getLayoutX() + prrTF.getPrefWidth() * 1.08);
                        sgHB.setPrefWidth(slLB.getPrefWidth() - SPrLB.getPrefWidth());
                        sgHB.setLayoutX(slLB.getLayoutX() + SPrLB.getPrefWidth());
                        set2.setPrefWidth(set1.getPrefWidth());
                        set2.setLayoutX(set1.getLayoutX());


                        SPeLB.setPrefWidth(newnum * 0.07);
                        SPeLB.setLayoutX(newnum / 70);
                        pulseFsLB.setPrefWidth(powerLBW.getPrefWidth());
                        pulsePsLB.setPrefWidth(powerLBW.getPrefWidth());
                        pulseTF.setPrefWidth(newnum / 6.125);
                        pulseTF.setLayoutX(SPeLB.getLayoutX() + SPeLB.getPrefWidth());
                        speHB.setPrefWidth(SPrLB.getPrefWidth() * 1.2);
                        speHB.setLayoutX(pulseTF.getLayoutX() + pulseTF.getPrefWidth() * 1.08);
                        pulseFsLB.setPrefWidth(powerLBW.getPrefWidth());
                        pulsePsLB.setPrefWidth(pulseFsLB.getPrefWidth());
                        pulseSd.setPrefWidth(slLB.getPrefWidth());
                        pulseSd.setLayoutX(SPeLB.getLayoutX());
                        set3.setPrefWidth(set1.getPrefWidth());
                        set3.setLayoutX(set1.getLayoutX());


                        SAVEBT.setPrefWidth(newnum / 6.75);
                        SAVEBT.setLayoutX(newnum / 2);
                        LOADBT.setPrefWidth(SAVEBT.getPrefWidth());
                        LOADBT.setLayoutX(SAVEBT.getLayoutX());
                        stopBT.setPrefWidth(SAVEBT.getPrefWidth());
                        stopBT.setLayoutX(SAVEBT.getLayoutX());

                        OPPLB.setPrefWidth(newnum / 14);
                        OPPLB.setLayoutX(SAVEBT.getLayoutX() + SAVEBT.getPrefWidth() * 1.5);
                        PRRLB.setPrefWidth(newnum / 14);
                        PRRLB.setLayoutX(SAVEBT.getLayoutX() + SAVEBT.getPrefWidth() * 1.5);
                        LB1.setPrefWidth(newnum / 8.2);
                        LB1.setLayoutX(OPPLB.getLayoutX() + OPPLB.getPrefWidth());
                        LB2.setPrefWidth(newnum / 8.2);
                        LB2.setLayoutX(OPPLB.getLayoutX() + OPPLB.getPrefWidth());
                        LogLB.setPrefWidth(LB1.getPrefWidth() / 5.7);
                        LogLB.setLayoutX(LB1.getLayoutX() + LB1.getPrefWidth() * 1.2);

                        DBLB.setPrefWidth(newnum);
                        double newnum1 = newnum / 7;
                        DBBox1.setPrefWidth(newnum1);
                        DBBox2.setPrefWidth(newnum1);
                        DBBox2.setLayoutX(DBBox1.getPrefWidth() * 1.02);
                        DBBox3.setPrefWidth(newnum1);
                        DBBox3.setLayoutX(DBBox2.getLayoutX() + DBBox2.getPrefWidth() * 1.02);
                        DBBox4.setPrefWidth(newnum1);
                        DBBox4.setLayoutX(DBBox3.getLayoutX() + DBBox3.getPrefWidth() * 1.02);

                        PHLV.setPrefWidth(newnum1 * 2);
                        PHLV.setLayoutX(DBBox4.getLayoutX() + DBBox4.getPrefWidth() * 1.02);
                        RELB.setPrefWidth(finalPane.getPrefWidth() / 14);
                        RELB.setLayoutX(PHLV.getLayoutX() + PHLV.getPrefWidth() * 1.07);
                        SingleLB.setPrefWidth(RELB.getPrefWidth() / 1.27);
                        SingleLB.setLayoutX(RELB.getLayoutX());
                        AUTOLB.setPrefWidth(SingleLB.getPrefWidth() / 1.375);
                        AUTOLB.setLayoutX(RELB.getLayoutX());
                        NumLB.setPrefWidth(SingleLB.getPrefWidth() / 1.67);
                        NumLB.setLayoutX(RELB.getLayoutX());
                        SULB.setPrefWidth(SingleLB.getPrefWidth() / 1.08);
                        SULB.setLayoutX(RELB.getLayoutX());

                        PD1.setPrefWidth(newnum1);
                        PD2.setPrefWidth(newnum1);
                        PD3.setPrefWidth(newnum1);
                        PD4.setPrefWidth(newnum1);
                        PD5.setPrefWidth(newnum1);
                        PD6.setPrefWidth(newnum1);
                        LD1.setPrefWidth(newnum1);
                        LD2.setPrefWidth(newnum1);
                        HP1.setPrefWidth(newnum1);
                        HP2.setPrefWidth(newnum1);
                        HP3.setPrefWidth(newnum1);
                        HP4.setPrefWidth(newnum1);
                        FAOM.setPrefWidth(newnum1);
                        SAOM.setPrefWidth(newnum1);
                        MOTOR.setPrefWidth(newnum1);
                        nu1.setPrefWidth(newnum1);
                        nu2.setPrefWidth(newnum1);
                        nu3.setPrefWidth(newnum1);


                        if (newnum > 1228) {
                            titleLB.setStyle("-fx-font-size: 40px;-fx-font-family: SimHei;");
                            RELB.setStyle("-fx-font-size: 19px");
                            SingleLB.setStyle("-fx-font-size: 21px;");
                            AUTOLB.setStyle("-fx-font-size: 17px;-fx-background-color:  rgb(35,253,35);");
                            NumLB.setStyle("-fx-font-size: 16px;-fx-background-color:  rgb(193,193,193);");
                        } else {
                            titleLB.setStyle("-fx-font-size: 33px;-fx-font-family: SimHei;");
                            RELB.setStyle("-fx-font-size: 16px");
                            SingleLB.setStyle("-fx-font-size: 18px;");
                            AUTOLB.setStyle("-fx-font-size: 14px;-fx-background-color:  rgb(35,253,35);");
                            NumLB.setStyle("-fx-font-size: 13px;-fx-background-color:  rgb(193,193,193);");
                        }

                        ECLB.setPrefWidth(newnum);
                        APLB.setPrefWidth(newnum);

                    }
                });
            }
        });
        // 总窗口长度监听
        stage.heightProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observableValue, Number oldnumber, Number newnumber) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println(newnumber.toString());
                        double newnum = (double) newnumber;
                        allpane.setPrefHeight(newnum);
                        lookup.setPrefHeight(newnum / 6.6941);
                        pane2.setPrefHeight(newnum / 7.375);
                        pane3.setPrefHeight(newnum / 3.612);
                        DBAD.setPrefHeight(newnum / 3.1466);
                        finalPane.setPrefHeight(DBAD.getPrefHeight());

                        pane2.setLayoutY(lookup.getPrefHeight() + 1);
                        pane3.setLayoutY(pane2.getLayoutY() + pane2.getPrefHeight());
                        DBAD.setLayoutY(pane3.getLayoutY() + pane3.getPrefHeight());


                        double prefHeight = lookup.getPrefHeight();
                        imglabel.setFitHeight(prefHeight / 1.48);
                        imglabel.setLayoutY(prefHeight * 0.15);
                        titleLB.setLayoutY(prefHeight * 0.3);
                        titleLB.setPrefHeight(prefHeight / 2.37);

                        connectLB.setLayoutY(prefHeight * 0.2);
                        usbBT.setPrefHeight(prefHeight * 0.31);
                        usbBT.setLayoutY((connectLB.getLayoutY() + connectLB.getHeight() * 1.5));
                        etBT.setPrefHeight(prefHeight * 0.31);
                        etBT.setLayoutY((connectLB.getLayoutY() + connectLB.getHeight() * 1.5));
                        rsBT.setPrefHeight(prefHeight * 0.31);
                        rsBT.setLayoutY((connectLB.getLayoutY() + connectLB.getHeight() * 1.5));

                        deLB.setPrefHeight(pane2.getPrefHeight() * 0.24);
                        ENGBT.setPrefHeight(deLB.getPrefHeight());
                        ENGXLB.setPrefHeight(deLB.getPrefHeight() * 3);
                        ENGXLB.setLayoutY(ENGBT.getPrefHeight());

                        MOLB.setPrefHeight(deLB.getPrefHeight());
                        MOLB.setLayoutY(deLB.getPrefHeight());
                        PNLB.setPrefHeight(deLB.getPrefHeight());
                        PNLB.setLayoutY(MOLB.getLayoutY() + MOLB.getPrefHeight());
                        SNLB.setPrefHeight(deLB.getPrefHeight());
                        SNLB.setLayoutY(PNLB.getLayoutY() + PNLB.getPrefHeight());

                        FWLB.setPrefHeight(deLB.getPrefHeight());
                        FWLB.setLayoutY(deLB.getPrefHeight());
                        HWLB.setPrefHeight(deLB.getPrefHeight());
                        HWLB.setLayoutY(FWLB.getLayoutY() + FWLB.getPrefHeight());
                        BLLB.setPrefHeight(deLB.getPrefHeight());
                        BLLB.setLayoutY(HWLB.getLayoutY() + HWLB.getPrefHeight());

                        STLB.setPrefHeight(ENGBT.getPrefHeight() * 4);
                        OKLB.setPrefHeight(ENGBT.getPrefHeight() * 4);

                        cpLB.setPrefHeight(pane3.getPrefHeight() * 0.125);
                        double prefHeight1 = cpLB.getPrefHeight();

                        SPrLB.setPrefHeight(prefHeight1);
                        SPrLB.setLayoutY(prefHeight1 * 1.2);
                        powerSd.setPrefHeight(SPrLB.getPrefHeight());
                        powerSd.setLayoutY(SPrLB.getLayoutY() + SPrLB.getPrefHeight() * 1.2);
                        powerTF.setPrefHeight(prefHeight1);
                        powerTF.setLayoutY(SPrLB.getLayoutY());
                        powerLBW.setPrefHeight(prefHeight1);
                        powerLBmW.setPrefHeight(prefHeight1);
                        powerLBdBm.setPrefHeight(prefHeight1);
                        sprHB.setPrefHeight(prefHeight1);
                        sprHB.setLayoutY(SPrLB.getLayoutY());
                        set1.setPrefHeight(prefHeight1);
                        set1.setLayoutY(SPrLB.getLayoutY());

                        SPRRLB.setPrefHeight(prefHeight1);
                        SPRRLB.setLayoutY(powerSd.getLayoutY() + SPrLB.getPrefHeight() * 1.2);
                        prrTF.setPrefHeight(powerTF.getPrefHeight());
                        prrTF.setLayoutY(SPRRLB.getLayoutY());
                        sprrHB.setPrefHeight(SPRRLB.getPrefHeight());
                        sprrHB.setLayoutY(SPRRLB.getLayoutY());
                        slLB.setPrefHeight(prefHeight1);
                        slLB.setLayoutY(SPRRLB.getLayoutY() + SPRRLB.getPrefHeight() * 1.2);
                        sgHB.setPrefHeight(slLB.getPrefHeight());
                        sgHB.setLayoutY(slLB.getLayoutY());
                        set2.setPrefHeight(prefHeight1);
                        set2.setLayoutY(SPRRLB.getLayoutY());


                        SPeLB.setPrefHeight(prefHeight1);
                        SPeLB.setLayoutY(slLB.getLayoutY() + SPRRLB.getPrefHeight() * 1.2);
                        pulseTF.setPrefHeight(prefHeight1);
                        pulseTF.setLayoutY(SPeLB.getLayoutY());
                        speHB.setPrefHeight(SPeLB.getPrefHeight());
                        speHB.setLayoutY(SPeLB.getLayoutY());
                        pulseFsLB.setPrefHeight(prefHeight1);
                        pulsePsLB.setPrefHeight(prefHeight1);
                        pulseSd.setPrefHeight(prefHeight1);
                        pulseSd.setLayoutY(SPeLB.getLayoutY() + SPeLB.getPrefHeight() * 1.2);
                        set3.setPrefHeight(prefHeight1);
                        set3.setLayoutY(SPeLB.getLayoutY());

                        SAVEBT.setPrefHeight(pane3.getPrefHeight() / 6.3);
                        SAVEBT.setLayoutY(SPrLB.getLayoutY() * 1.2);
                        LOADBT.setPrefHeight(SAVEBT.getPrefHeight());
                        LOADBT.setLayoutY(SAVEBT.getLayoutY() + SAVEBT.getPrefHeight() * 1.2);
                        stopBT.setPrefHeight(pane3.getPrefHeight() / 2.8);
                        stopBT.setLayoutY(LOADBT.getLayoutY() + LOADBT.getPrefHeight() * 1.5);

                        OPPLB.setPrefHeight(OPPLB.getPrefWidth());
                        OPPLB.setLayoutY(SAVEBT.getLayoutY());
                        PRRLB.setPrefHeight(OPPLB.getPrefHeight());
                        PRRLB.setLayoutY(stopBT.getLayoutY());
                        LB1.setPrefHeight(pane3.getPrefHeight() / 3.3);
                        LB1.setLayoutY(OPPLB.getLayoutY());
                        LB2.setPrefHeight(LB1.getPrefHeight());
                        LB2.setLayoutY(PRRLB.getLayoutY());

                        DBLB.setPrefHeight(prefHeight1);
                        DBBox1.setPrefHeight(finalPane.getPrefHeight() / 1.67);
                        DBBox1.setLayoutY(deLB.getPrefHeight() * 1.3);
                        DBBox2.setPrefHeight(DBBox1.getPrefHeight());
                        DBBox2.setLayoutY(DBBox1.getLayoutY());
                        DBBox3.setPrefHeight(DBBox1.getPrefHeight());
                        DBBox3.setLayoutY(DBBox1.getLayoutY());
                        DBBox4.setPrefHeight(DBBox1.getPrefHeight());
                        DBBox4.setLayoutY(DBBox1.getLayoutY());

                        PHLV.setLayoutY(DBBox1.getLayoutY());
                        PHLV.setPrefHeight(DBBox1.getPrefHeight());
                        RELB.setPrefHeight(DBLB.getPrefHeight());
                        RELB.setLayoutY(DBLB.getPrefHeight() * 1.4);
                        SingleLB.setPrefHeight(DBLB.getPrefHeight());
                        SingleLB.setLayoutY(RELB.getLayoutY() + RELB.getPrefHeight() * 1.2);
                        AUTOLB.setPrefHeight(RELB.getPrefHeight() / 1.21);
                        AUTOLB.setLayoutY(SingleLB.getLayoutY() + SingleLB.getPrefHeight() * 1.4);
                        NumLB.setPrefHeight(RELB.getPrefHeight());
                        NumLB.setLayoutY(AUTOLB.getLayoutY() + AUTOLB.getPrefHeight() * 1.1);
                        SULB.setPrefHeight(RELB.getPrefHeight() / 1.53);
                        SULB.setLayoutY(NumLB.getLayoutY() + NumLB.getPrefHeight());


                        double newnum1 = DBLB.getPrefHeight();
                        PD1.setPrefHeight(newnum1);
                        PD2.setPrefHeight(newnum1);
                        PD3.setPrefHeight(newnum1);
                        PD4.setPrefHeight(newnum1);
                        PD5.setPrefHeight(newnum1);
                        PD6.setPrefHeight(newnum1);
                        LD1.setPrefHeight(newnum1);
                        LD2.setPrefHeight(newnum1);
                        HP1.setPrefHeight(newnum1);
                        HP2.setPrefHeight(newnum1);
                        HP3.setPrefHeight(newnum1);
                        HP4.setPrefHeight(newnum1);
                        FAOM.setPrefHeight(newnum1);
                        SAOM.setPrefHeight(newnum1);
                        MOTOR.setPrefHeight(newnum1);
                        nu1.setPrefHeight(newnum1);
                        nu2.setPrefHeight(newnum1);
                        nu3.setPrefHeight(newnum1);

                        ECLB.setPrefHeight(prefHeight1);
                        ECLB.setLayoutY(DBBox1.getLayoutY() + DBBox1.getPrefHeight() + DBLB.getPrefHeight() * 0.25);
                        APLB.setPrefHeight(prefHeight1);
                        APLB.setLayoutY(DBAD.getLayoutY() + DBAD.getPrefHeight());
                    }
                });
            }
        });

        // 菜单各选项的Action事件
        ObservableList<Menu> menus = EGMB.getMenus();
        for (Menu menu : menus) {
            ObservableList<MenuItem> items = menu.getItems();
            for (MenuItem item : items) {
                String text = item.getText();
                /* todo 工程模式和日志列表窗口 */
                if (text.equals("重启设备")) {
                    item.setOnAction(new EventHandler<ActionEvent>() {
                        @Override
                        public void handle(ActionEvent actionEvent) {
                            // 参考boot指令
                            if (port != null && port.isOpen()) {
                                configurationMap.put("BOOT", "");
                            }
                        }
                    });
                }
                if (text.equals("恢复出厂设置")) {
                    item.setOnAction(new EventHandler<ActionEvent>() {
                        @Override
                        public void handle(ActionEvent actionEvent) {
                            // 参考RST指令
                            if (port != null && port.isOpen()) {
                                configurationMap.put("RST", "");
                            }
                        }
                    });
                }
                if (text.equals("帮助文档")) {
                    item.setOnAction(new EventHandler<ActionEvent>() {
                        @Override
                        public void handle(ActionEvent actionEvent) {
                            if (stage1 != null) {
                                stage1.show();
                                return;
                            }
                            stage1 = new Stage();
                            Group group = new Group();
                            Scene scene1 = new Scene(group);
                            Text text1 = new Text();
                            text1.setLayoutY(10);
                            text1.setText("Step1 - 正常安放好激光器主机箱和水冷机、电源等外设\n" +
                                    "Step2 - 用快速接头连接激光器水路，电源线，通信接口\n" +
                                    "Step3 - 打开水冷机，启动水冷循环\n" +
                                    "Step4 - 按下电源按钮使激光器主控板上电，风扇自动启动，系统上电后自动进入自检程序\n" +
                                    "Step5 - 启动iNFA_UserApp，配置好通信接口，读取机器识别信息；机器完成自检后，自动进入Standby状态\n" +
                                    "Step6 - 设置激光工作参数（设置重频和输出平均功率）\n" +
                                    "Step7 - 打开机箱背板上的激光泵浦电源钥匙开关\n" +
                                    "Step8 - 在UserApp上打开激光器（GO），激光器进入Presetting，然后启动自动算法优化输出性能(Optimizing)，完成优化后进入正常输出状态（OK）。\n" +
                                    "Step9 - 关闭激光器输出（STOP），关闭激光泵浦电源钥匙开关\n" +
                                    "Step10 - 关闭激光器电源按钮，关闭水路循环，关闭水冷机");
                            group.getChildren().add(text1);
                            stage1.setResizable(false);
                            stage1.setScene(scene1);
                            stage1.show();
                            // 可关联iNFA激光器使用说明书
                        }
                    });
                }
                if (text.equals("关于爱欧光学")) {
                    item.setOnAction(new EventHandler<ActionEvent>() {
                        @Override
                        public void handle(ActionEvent actionEvent) {
                            try {
                                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://www.aioptics.com/");
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    });
                }
            }
        }

        // ENG MODE按钮点击事件
        ENGBT.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (port != null && port.isOpen() && resultList.size() > 0 && resultList.get(1) != "") {
                    if (newstage != null) {
                        newstage.show();
                        return;
                    }
                    newstage = new Stage();
                    GridPane gridPane = new GridPane();
                    Label label = new Label("工程模式");
                    Font font = Font.font("SimHei", FontWeight.BOLD, 22);
                    label.setFont(font);
                    label.setPrefWidth(250);
                    label.setAlignment(Pos.CENTER);
                    PasswordField textField = new PasswordField();
                    textField.setFocusTraversable(false);
                    textField.setPromptText("请输入密码访问工程模式");
                    textField.setPrefWidth(250);
                    gridPane.add(label, 0, 0);
                    gridPane.add(textField, 0, 1);
                    gridPane.setAlignment(Pos.CENTER);
                    gridPane.setVgap(10);
                    Scene scene = new Scene(gridPane);
                    newstage.setTitle("工程模式密码输入");
                    newstage.setScene(scene);
                    newstage.setWidth(300);
                    newstage.setHeight(120);
                    newstage.setResizable(false);
                    newstage.show();
                    textField.setOnAction(new EventHandler<ActionEvent>() {
                        @Override
                        public void handle(ActionEvent actionEvent) {
                            String password = textField.getText();
                            if (password.equals("123456")) {
                                new Alert(Alert.AlertType.INFORMATION, "登录成功！").showAndWait();
                                stage4 = new Stage();
                                Label label1 = new Label("上次维护校准时期");
                                Label label2 = new Label("出厂日期：XXXXXXX");
                                for (String s : resultList) {
                                    String[] split = s.split(":");
                                    if (split.length > 1) {
                                        switch (split[0].trim()) {
                                            case "Product Date":
                                                Platform.runLater(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        label1.setText("上次维护校准时期：" + split[1].trim());
                                                    }
                                                });
                                                break;
                                        }
                                    }
                                }
                                label1.setAlignment(Pos.CENTER);
                                label2.setAlignment(Pos.CENTER);
                                GridPane gridPane1 = new GridPane();
                                gridPane1.setPrefWidth(300);
                                gridPane1.setPrefHeight(100);
                                gridPane1.add(label1, 0, 0);
                                gridPane1.add(label2, 0, 1);
                                gridPane1.setAlignment(Pos.CENTER);
                                gridPane1.setVgap(10);
                                Scene scene1 = new Scene(gridPane1);
                                stage4.setScene(scene1);
                                newstage.close();
                                stage4.setResizable(false);
                                stage4.show();
                            } else {
                                new Alert(Alert.AlertType.ERROR, "密码错误").showAndWait();
                            }
                        }
                    });
                }
            }
        });
        // LOAD按钮的点击事件
        LOADBT.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (port != null && port.isOpen()) {
//                    ClassPathResource resource = new ClassPathResource("Configuration.txt");
                    String path = System.getProperty("user.dir") + "/Configuration.txt";
                    File file = new File(path);
                    if (!file.exists() && !file.isDirectory()) {
                        file.getParentFile().mkdirs();
                        try {
                            file.createNewFile();
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    try {
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                        String s = null;
                        while ((s = bufferedReader.readLine()) != null) {
                            String[] split = s.split(":");
                            if (split.length > 1) {
                                switch (split[0].trim()) {
                                    case "LASER PSP":
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                //更新JavaFX的主线程的代码放在此处
                                                powerTF.setText(split[1].trim());
                                                Double v = Double.parseDouble(split[1].trim());
                                                Event.fireEvent(powerLBW, new MouseEvent(MouseEvent.MOUSE_CLICKED, 1, 1, 1, 1, MouseButton.PRIMARY, 1, true, true, true, true, true, true, true, true, true, true, null));
                                                powerSd.setValue(v);
                                                powerTF.setText(v.toString());
                                            }
                                        });
                                        break;
                                    case "LASER FRQ":
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                //更新JavaFX的主线程的代码放在此处
                                                String[] s1 = split[1].trim().split(" ");
                                                Double v = Double.parseDouble(s1[0].trim());
                                                Label label = null;
                                                if (v == 10) {
                                                    label = ((Label) sgHB.lookup("#singleLB10Hz"));
                                                } else if (v == 100) {
                                                    label = ((Label) sgHB.lookup("#singleLB100Hz"));
                                                } else if (v == 1000) {
                                                    v = v / 1000;
                                                    label = ((Label) sgHB.lookup("#singleLB1KHz"));
                                                } else if (v == 10000) {
                                                    v = v / 1000;
                                                    label = ((Label) sgHB.lookup("#singleLB10KHz"));
                                                } else if (v == 100000) {
                                                    v = v / 1000;
                                                    label = ((Label) sgHB.lookup("#singleLB100KHz"));
                                                } else if (v == 1000000) {
                                                    v = v / 1000000;
                                                    label = ((Label) sgHB.lookup("#singleLB1MHz"));
                                                } else {
                                                    ObservableList<Node> children = sgHB.getChildren();
                                                    for (Node child : children) {
                                                        if (child.getStyle().equals("-fx-background-color: rgb(16, 253, 16)")) {
                                                            child.setStyle("-fx-background-color: rgb(208,206,206)");
                                                        }
                                                    }
                                                }
                                                if (label != null) {
                                                    Event.fireEvent(label, new MouseEvent(MouseEvent.MOUSE_CLICKED, 1, 1, 1, 1, MouseButton.PRIMARY, 1, true, true, true, true, true, true, true, true, true, true, null));
                                                }
                                                prrTF.setText(v.toString());
                                            }
                                        });
                                        break;
                                }
                            }
                        }
                        bufferedReader.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        // USB按钮的点击事件
        usbBT.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (usbBT.getStyle().equals("-fx-background-color: rgb(143,170,220)")) {
                    return;
                }
                usbBT.setStyle("-fx-background-color: rgb(143,170,220)");
                rsBT.setStyle("-fx-background-color:  rgb(47,85,151)");
                etBT.setStyle("-fx-background-color:  rgb(47,85,151)");
                FXMLLoader fxmlLoader1 = new FXMLLoader();
                if (stage2 != null) {
                    stage2.show();
                    return;
                }
                stage2 = new Stage();
                stage2.setResizable(false);
                try {
                    Scene scene1 = new Scene(fxmlLoader1.load(getClass().getResource("serialPortConfiguration.fxml")));

                    ChoiceBox CKCB = ((ChoiceBox) scene1.lookup("#CKCB"));
                    ChoiceBox StopBitCB = ((ChoiceBox) scene1.lookup("#StopBitCB"));
                    ChoiceBox ParityCB = ((ChoiceBox) scene1.lookup("#ParityCB"));
                    ChoiceBox DataBitCB = ((ChoiceBox) scene1.lookup("#DataBitCB"));
                    ImageView shuaxin = (ImageView) scene1.lookup("#shuaxin");

                    TextField BotTF = (TextField) scene1.lookup("#BotTF");

                    Button connectBT = (Button) scene1.lookup("#connectBT");


                    for (SerialPort serialPort : serialPorts) {
                        String systemPortName = serialPort.getSystemPortName();
                        CKCB.getItems().add(systemPortName);
                    }
                    if (serialPorts.length > 0) {
                        CKCB.setValue(serialPorts[0].getSystemPortName());
                    }
                    StopBitCB.getItems().add(1);
                    StopBitCB.getItems().add(1.5);
                    StopBitCB.getItems().add(2);
                    StopBitCB.setValue(1);

                    ParityCB.getItems().add("无");
                    ParityCB.getItems().add("奇校验");
                    ParityCB.getItems().add("偶校验");
                    ParityCB.setValue("无");

                    DataBitCB.getItems().add(8);
                    DataBitCB.getItems().add(7);
                    DataBitCB.getItems().add(6);
                    DataBitCB.getItems().add(5);
                    DataBitCB.setValue(8);

                    shuaxin.setOnMouseClicked(new EventHandler<MouseEvent>() {
                        @Override
                        public void handle(MouseEvent mouseEvent) {
                            serialPorts = SerialPort.getCommPorts();
                            for (SerialPort serialPort : serialPorts) {
                                String systemPortName = serialPort.getSystemPortName();
                                List list = CKCB.getItems().stream().toList();
                                if (!list.contains(systemPortName)) {
                                    CKCB.getItems().add(systemPortName);
                                }
                            }
                        }
                    });

                    connectBT.setOnMouseClicked(new EventHandler<MouseEvent>() {
                        @Override
                        public void handle(MouseEvent mouseEvent) {
                            Object value = CKCB.getValue();
                            if (value == null) {
                                return;
                            }
                            Integer i = Integer.parseInt(BotTF.getText());
                            Integer value1 = Integer.parseInt(DataBitCB.getValue().toString());

                            Integer stopbit = null;
                            switch (StopBitCB.getValue().toString()) {
                                case "1":
                                    stopbit = 1;
                                    break;
                                case "1.5":
                                    stopbit = 2;
                                    break;
                                case "2":
                                    stopbit = 3;
                            }
                            Integer parity = null;
                            switch (ParityCB.getValue().toString()) {
                                case "无":
                                    parity = 0;
                                    break;
                                case "奇校验":
                                    parity = 1;
                                    break;
                                case "偶校验":
                                    parity = 2;
                                    break;
                            }
                            for (SerialPort serialPort : serialPorts) {
                                if (serialPort.getSystemPortName().equals(value.toString())) {
                                    serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING | SerialPort.TIMEOUT_WRITE_BLOCKING, 1000, 1000);//设置超时
                                    serialPort.setRTS();//设置RTS。也可以设置DTR
                                    serialPort.setFlowControl(SerialPort.FLOW_CONTROL_DISABLED);
                                    System.out.println(i);
                                    serialPort.setComPortParameters(i, value1, stopbit, parity);
                                    if (!serialPort.isOpen()) {
                                        boolean isCommOpeded = serialPort.openPort();//判断串口是否打开，如果没打开，就打开串口。打开串口的函数会返回一个boolean值，用于表明串口是否成功打开了
                                    }
                                    port = serialPort;
                                }
                            }
                            if (port.isOpen()) {
                                timer2 = new Timer();
                                timerTask2 = new TimerTask() {
                                    @Override
                                    public void run() {
                                        CopyOnWriteArrayList<String> strings = new CopyOnWriteArrayList<>();
                                        ArrayList<String> instructionList = new ArrayList<>();
                                        if (configurationMap.size() > 0) {
                                            for (String s : configurationMap.keySet()) {
                                                instructionList.add(s + configurationMap.get(s));
                                            }
                                            configurationMap = new HashMap<>();
                                        }
                                        instructionList.add("ver");
                                        instructionList.add("ver full");
                                        instructionList.add("MST");
                                        instructionList.add("laser");
                                        instructionList.add("pd");
                                        instructionList.add("ths");
                                        instructionList.add("pump 1 ild");
                                        instructionList.add("pump 2 ild");
                                        instructionList.add("HIPUMP 1 ILD");
                                        instructionList.add("HIPUMP 2 ILD");
                                        instructionList.add("FAOM TRANS");
                                        instructionList.add("FAOM BLOCK");
                                        instructionList.add("SAOM TRANS");
                                        instructionList.add("SAOM BLOCK");
                                        instructionList.add("MOTO");
                                        instructionList.add("AST");
                                        for (String s : instructionList) {
//                                            port.writeBytes((s+"\n").getBytes(), (s+"\n").getBytes().length);  // 真实发送结尾
                                            port.writeBytes((s).getBytes(), (s).getBytes().length);
                                            try {
                                                Thread.sleep(100);
                                            } catch (InterruptedException e) {
                                                throw new RuntimeException(e);
                                            }
                                            String msg = "";
                                            while (port.bytesAvailable() > 0) {
                                                byte[] newData = new byte[port.bytesAvailable()];
                                                int numRead = port.readBytes(newData, newData.length);
                                                String newDataString = new String(newData);
                                                msg = msg + newDataString;
                                            }
//                                            String[] split = msg.split("\r\n");  // 真实分隔符
                                            String[] split = msg.split("\n");
                                            for (String s1 : split) {
                                                strings.add(s1);
                                            }
                                            if (msg == "") {
                                                if (Max_time != null) {
                                                    if (Max_time <= System.currentTimeMillis()) {
                                                        Platform.runLater(new Runnable() {
                                                            @Override
                                                            public void run() {
                                                                usbBT.setStyle("-fx-background-color:  rgb(47,85,151)");
                                                            }
                                                        });
                                                        port.closePort();
                                                        timer2.cancel();
                                                    }
                                                }
                                            } else {
                                                Max_time = System.currentTimeMillis() + 10000;
                                            }
                                        }
                                        resultList = strings;
                                    }
                                };
                                timer2.schedule(timerTask2, 0, 200);
                                long max_Time = System.currentTimeMillis() + 10000;
                                while (true) {
                                    if (!resultList.isEmpty()) {
                                        if (resultList.get(1) != "") {
                                            // 加载机器上的配置
                                            for (String s : resultList) {
                                                String[] split = s.split(":");
                                                switch (split[0]) {
                                                    case "Firmware Vers":
                                                        Platform.runLater(new Runnable() {
                                                            @Override
                                                            public void run() {
                                                                if (split[1].lastIndexOf("(") > 0) {
                                                                    split[1] = split[1].substring(0, split[1].lastIndexOf("("));
                                                                }
                                                                //更新JavaFX的主线程的代码放在此处
                                                                FWLB.setText("  FW version :" + split[1]);
                                                            }
                                                        });
                                                        break;
                                                    case "PL Vers":
                                                        Platform.runLater(new Runnable() {
                                                            @Override
                                                            public void run() {
                                                                //更新JavaFX的主线程的代码放在此处
                                                                HWLB.setText("  HW version :" + split[1].trim());
                                                            }
                                                        });
                                                        break;
                                                    case "MST":
                                                        Platform.runLater(new Runnable() {
                                                            @Override
                                                            public void run() {
                                                                //更新JavaFX的主线程的代码放在此处
                                                                if (split.length > 1) {
                                                                    switch (split[1].trim().toUpperCase()) {
                                                                        case "STD":
                                                                            OKLB.setText("Standby");
                                                                            break;
                                                                        case "BYS":
                                                                            OKLB.setText("Optizing");
                                                                            break;
                                                                        case "WRN":
                                                                            OKLB.setText("Safety");
                                                                            break;
                                                                        case "ERR":
                                                                            OKLB.setText("Error");
                                                                            break;
                                                                        case "OK":
                                                                            OKLB.setText("OK");
                                                                    }
                                                                }
                                                            }
                                                        });
                                                        break;
                                                    case "LASER PSP":
                                                        Platform.runLater(new Runnable() {
                                                            @Override
                                                            public void run() {
                                                                //更新JavaFX的主线程的代码放在此处
                                                                powerTF.setText(split[1].trim());
                                                                Double v = Double.parseDouble(split[1].trim());
                                                                Event.fireEvent(powerLBW, new MouseEvent(MouseEvent.MOUSE_CLICKED, 1, 1, 1, 1, MouseButton.PRIMARY, 1, true, true, true, true, true, true, true, true, true, true, null));
                                                                powerSd.setValue(v);
                                                                powerTF.setText(v.toString());
                                                            }
                                                        });
                                                        break;
                                                    case "LASER FRQ":
                                                        Platform.runLater(new Runnable() {
                                                            @Override
                                                            public void run() {
                                                                //更新JavaFX的主线程的代码放在此处
                                                                String[] s1 = split[1].trim().split(" ");
                                                                Double v = Double.parseDouble(s1[0].trim());
                                                                Event.fireEvent(prrLBHz, new MouseEvent(MouseEvent.MOUSE_CLICKED, 1, 1, 1, 1, MouseButton.PRIMARY, 1, true, true, true, true, true, true, true, true, true, true, null));
                                                                prrTF.setText(v.toString());
                                                            }
                                                        });
                                                        break;
                                                    case "LASER EN":
                                                        Platform.runLater(new Runnable() {
                                                            @Override
                                                            public void run() {
                                                                //更新JavaFX的主线程的代码放在此处
                                                                switch (split[1].toUpperCase().trim()) {
                                                                    case "OFF":
                                                                        stopBT.setText("GO");
                                                                        break;
                                                                    case "ON":
                                                                        stopBT.setText("STOP");
                                                                        break;
                                                                }
                                                            }
                                                        });
                                                        break;
                                                    case "Part Number":
                                                        Platform.runLater(new Runnable() {
                                                            @Override
                                                            public void run() {
                                                                PNLB.setText("  P/N :" + split[1]);
                                                            }
                                                        });
                                                        break;
                                                    case "Model Number":
                                                        Platform.runLater(new Runnable() {
                                                            @Override
                                                            public void run() {
                                                                MOLB.setText("  Model :" + split[1]);
                                                            }
                                                        });
                                                        break;
                                                    case "Serial Number":
                                                        Platform.runLater(new Runnable() {
                                                            @Override
                                                            public void run() {
                                                                SNLB.setText("  S/N :" + split[1]);
                                                            }
                                                        });
                                                        break;
                                                }
                                            }
                                            break;
                                        } else {
                                            resultList = new CopyOnWriteArrayList<>();
                                        }
                                    }
                                    if (System.currentTimeMillis() > max_Time) {
                                        port.closePort();
                                        if (timer2 != null) {
                                            timer2.cancel();
                                        }
                                        new Alert(Alert.AlertType.ERROR, "连接失败").showAndWait();
                                        usbBT.setStyle("-fx-background-color:  rgb(47,85,151)");
                                        break;
                                    }
                                }
                                stage2.close();
                                if (!resultList.isEmpty() && resultList.get(1) != "") {
                                    new Alert(Alert.AlertType.INFORMATION, "连接成功").showAndWait();
                                }
                            } else {
                                OKLB.setText("DISCONNECT");
                            }
                        }
                    });
                    stage2.setScene(scene1);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                stage2.show();
                stage2.setOnCloseRequest(new EventHandler<WindowEvent>() {
                    @Override
                    public void handle(WindowEvent windowEvent) {
                        if (port == null || !port.isOpen()) {
                            Platform.runLater(new Runnable() {
                                @Override
                                public void run() {
                                    usbBT.setStyle("-fx-background-color: rgb(47,85,151)");
                                }
                            });
                        }
                    }
                });
            }
        });
        // 重频设置按钮点击事件
        set2.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (port != null && port.isOpen()) {
                    if (prrTF.getText().trim().length() > 0) {
                        if (stopBT.getText().equals("STOP")) {
                            new Alert(Alert.AlertType.ERROR, "只能在激光器关闭状态下才可设置!").showAndWait();
                        } else {
                            ObservableList<Node> children = sprrHB.getChildren();
                            Double v = Double.parseDouble(prrTF.getText());
                            Double v1 = 0.0;
                            for (Node child : children) {
                                String style = child.getStyle();
                                if (style.equals("-fx-background-color: rgb(16, 253, 16)")) {
                                    Label child1 = (Label) child;
                                    switch (child1.getText().toUpperCase()) {
                                        case "HZ":
                                            v1 = v;
                                            break;
                                        case "KHZ":
                                            v1 = v * 1000;
                                            break;
                                        case "MHZ":
                                            v1 = v * 1000000;
                                            break;
                                    }
                                }
                            }
                            if (v1 > 1000000) {
                                new Alert(Alert.AlertType.ERROR, "最大不能超过1MHz").showAndWait();
                            } else {
                                configurationMap.put("LASER FRQ ", v1.toString());
                            }
                        }
                    }
                }
            }
        });
        // 激光器功率设置
        set1.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (port != null && port.isOpen()) {
                    if (powerTF.getText().trim().length() > 0) {
                        ObservableList<Node> children = sprHB.getChildren();
                        Double v = Double.parseDouble(powerTF.getText());
                        Double v1 = 0.0;
                        for (Node child : children) {
                            String style = child.getStyle();
                            if (style.equals("-fx-background-color: rgb(16, 253, 16)")) {
                                Label child1 = (Label) child;
                                switch (child1.getText().toUpperCase()) {
                                    case "W":
                                        v1 = v;
                                        break;
                                    case "MW":
                                        v1 = v / 1000;
                                        break;
                                    case "DBM":
                                        v1 = Math.pow(10, v / 10) / 1000;
                                }
                            }
                        }
                        if (v1 > 20) {
                            new Alert(Alert.AlertType.ERROR, "最大不能超过20W").showAndWait();
                        } else {
                            configurationMap.put("LASER PSP ", v1.toString());
                        }
                    }
                }
            }
        });
        // 激光器开启关闭按钮点击事件
        stopBT.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (port != null && port.isOpen()) {
                    if (stopBT.getText().trim().equals("STOP")) {
                        configurationMap.put("LASER EN ", "OFF");
                    } else {
                        configurationMap.put("LASER EN ", "ON");
                    }
                }
            }
        });
        // 保存设置按钮点击事件
        SAVEBT.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                ArrayList<String> a = new ArrayList<>();
                if (port != null && port.isOpen()) {
                    if (powerTF.getText().trim().length() > 0) {
                        ObservableList<Node> children = sprHB.getChildren();
                        Double v = Double.parseDouble(powerTF.getText());
                        Double v1 = 0.0;
                        for (Node child : children) {
                            String style = child.getStyle();
                            if (style.equals("-fx-background-color: rgb(16, 253, 16)")) {
                                Label child1 = (Label) child;
                                switch (child1.getText().toUpperCase()) {
                                    case "W":
                                        v1 = v;
                                        break;
                                    case "MW":
                                        v1 = v / 1000;
                                        break;
                                    case "DBM":
                                        v1 = Math.pow(10, v / 10) / 1000;
                                }
                            }
                        }
                        if (v1 > 20) {
                            new Alert(Alert.AlertType.ERROR, "最大不能超过20W").showAndWait();
                        } else {
                            a.add("LASER PSP :" + String.format("%.2f", v1));
                        }
                    }
                    if (prrTF.getText().trim().length() > 0) {
                        if (stopBT.getText().equals("STOP")) {
                            new Alert(Alert.AlertType.ERROR, "只能在激光器关闭状态下才可设置!").showAndWait();
                        } else {
                            ObservableList<Node> children = sprrHB.getChildren();
                            Double v = Double.parseDouble(prrTF.getText());
                            Double v1 = 0.0;
                            for (Node child : children) {
                                String style = child.getStyle();
                                if (style.equals("-fx-background-color: rgb(16, 253, 16)")) {
                                    Label child1 = (Label) child;
                                    switch (child1.getText().toUpperCase()) {
                                        case "HZ":
                                            v1 = v;
                                            break;
                                        case "KHZ":
                                            v1 = v * 1000;
                                            break;
                                        case "MHZ":
                                            v1 = v * 1000000;
                                            break;
                                    }
                                }
                            }
                            if (v1 > 1000000) {
                                new Alert(Alert.AlertType.ERROR, "最大不能超过1MHz").showAndWait();
                            } else {
                                a.add("LASER FRQ :" + v1.toString());
                            }
                        }
                    }
                    if (a.size() > 1) {
                        String path = System.getProperty("user.dir") + "/Configuration.txt";
                        File file = new File(path);
                        if (!file.exists() && !file.isDirectory()) {
                            file.getParentFile().mkdirs();
                        }
                        try {
                            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
                            for (String s : a) {
                                bufferedWriter.write(s + "\n");
                                bufferedWriter.flush();
                            }
                            bufferedWriter.close();
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        });
        // Dash Board一栏展开关闭监听事件
        DBAD.expandedPaneProperty().addListener(new ChangeListener<TitledPane>() {
            @Override
            public void changed(ObservableValue<? extends TitledPane> observableValue, TitledPane titledPane, TitledPane t1) {
                if (t1 != null) {
                    timer1 = new Timer();
                    timerTask1 = new TimerTask() {
                        @Override
                        public void run() {
                            String flag = "";
                            String flag1 = "";
                            ArrayList<String> items = new ArrayList<>();
                            ArrayList<String> strings = new ArrayList<>();
                            for (String s : resultList) {
                                String[] split = s.split(":");
                                if (split.length > 1) {
                                    switch (split[0].trim()) {
                                        case "PD 1":
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    System.out.println(split[0] + split[1]);
                                                    PD1.setText("  PD1: " + split[1]);
                                                }
                                            });
                                            break;
                                        case "PD 2":
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    PD2.setText("  PD2: " + split[1]);
                                                }
                                            });
                                            break;
                                        case "PD 3":
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    PD3.setText("  PD3: " + split[1]);
                                                }
                                            });
                                            break;
                                        case "PD 4":
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    PD4.setText("  PD4: " + split[1]);
                                                }
                                            });
                                            break;
                                        case "PD 5":
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    PD5.setText("  PD5: " + split[1]);
                                                }
                                            });
                                            break;
                                        case "PD 6":
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    PD6.setText("  PD6: " + split[1]);
                                                }
                                            });
                                            break;
                                        case "HIDRV1":
                                        case "HIDEV1":
                                        case "HIDRV2":
                                        case "HIDEV2":
                                        case "PDBA":
                                        case "PUMP1":
                                        case "PUMP2":
                                        case "SP1":
                                        case "SP2":
                                        case "SP3":
                                        case "PSB":
                                        case "MAIN":
                                            items.add(split[0].trim() + " : " + split[1].trim());
                                            break;
                                        case "PUMP 1 ILD":
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    LD1.setText("  LD1:  " + split[1]);
                                                    HP3.setText("  HP3:  " + " 0 A");
                                                    HP4.setText("  HP4:  " + " 0 A");
                                                }
                                            });
                                            break;
                                        case "PUMP 2 ILD":
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    LD2.setText("  LD2:  " + split[1]);
                                                }
                                            });
                                            break;
                                        case "HIPUMP 1 ILD":
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    HP1.setText("  HP1:  " + split[1]);
                                                }
                                            });
                                            break;
                                        case "HIPUMP 2 ILD":
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    HP2.setText("  HP2:  " + split[1]);
                                                }
                                            });
                                            break;
                                        case "HIPUMP 3 ILD":
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    HP3.setText("  HP3:  " + split[1]);
                                                }
                                            });
                                            break;
                                        case "HIPUMP 4 ILD":
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    HP4.setText("  HP4:  " + split[1]);
                                                }
                                            });
                                            break;
                                        case "FAOM TRANS":
                                            if (flag == "") {
                                                flag = split[1].trim().toUpperCase();
                                            } else if (flag.toUpperCase().equals("OFF") && split[1].trim().toUpperCase().equals("OFF")) {
                                                Platform.runLater(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        FAOM.setText("  FAOM:  " + "PWM");
                                                    }
                                                });
                                            } else {
                                                String s1 = "";
                                                if (flag.equals("OFF") && split[1].trim().toUpperCase().equals("ON")) {
                                                    s1 = "全通模式";
                                                } else if (flag.equals("ON") && split[1].trim().toUpperCase().equals("OFF")) {
                                                    s1 = "关断模式";
                                                }
                                                String finalS = s1;
                                                Platform.runLater(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        FAOM.setText("  FAOM:  " + finalS);
                                                    }
                                                });

                                            }
                                            break;
                                        case "FAOM BLOCK":
                                            if (flag == "") {
                                                flag = split[1].trim().toUpperCase();
                                            } else if (flag.toUpperCase().equals("OFF") && split[1].trim().toUpperCase().equals("OFF")) {
                                                Platform.runLater(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        FAOM.setText("  FAOM:  " + "PWM");
                                                    }
                                                });
                                            } else {
                                                String s1 = "";
                                                if (flag.equals("OFF") && split[1].trim().toUpperCase().equals("ON")) {
                                                    s1 = "关断模式";
                                                } else if (flag.equals("ON") && split[1].trim().toUpperCase().equals("OFF")) {
                                                    s1 = "全通模式";
                                                }

                                                String finalS = s1;
                                                Platform.runLater(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        FAOM.setText("  FAOM:  " + finalS);
                                                    }
                                                });

                                            }
                                            break;
                                        case "SAOM TRANS":
                                            if (flag1 == "") {
                                                flag1 = split[1].trim().toUpperCase();
                                            } else if (flag1.equals(split[1].trim().toUpperCase())) {
                                                if (flag1.equals("OFF")) {
                                                    Platform.runLater(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            FAOM.setText("  FAOM:  " + "PWM");
                                                        }
                                                    });
                                                }
                                            } else {
                                                String s1 = "";
                                                if (flag1.equals("OFF") && split[1].trim().toUpperCase().equals("ON")) {
                                                    s1 = "全通模式";
                                                } else if (flag1.equals("ON") && split[1].trim().toUpperCase().equals("OFF")) {
                                                    s1 = "关断模式";
                                                }
                                                String finalS = s1;
                                                Platform.runLater(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        SAOM.setText("  SAOM:  " + finalS);
                                                    }
                                                });

                                            }
                                            break;
                                        case "SAOM BLOCK":
                                            if (flag1 == "") {
                                                flag1 = split[1].trim().toUpperCase();
                                            } else if (flag1.equals(split[1].trim().toUpperCase())) {
                                                if (flag1.equals("OFF")) {
                                                    Platform.runLater(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            SAOM.setText("  SAOM:  " + "PWM");
                                                        }
                                                    });
                                                }
                                            } else {
                                                String s1 = "";
                                                if (flag1.equals("OFF") && split[1].trim().toUpperCase().equals("ON")) {
                                                    s1 = "关断模式";
                                                } else if (flag1.equals("ON") && split[1].trim().toUpperCase().equals("OFF")) {
                                                    s1 = "全通模式";
                                                }
                                                String finalS = s1;
                                                Platform.runLater(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        SAOM.setText("  SAOM:  " + finalS);
                                                    }
                                                });
                                            }
                                            break;
                                        case "MOTO MODE":
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    if (split[1].trim().toUpperCase().equals("A")) {
                                                        MOTOR.setText("  MOTOR:  " + "自动模式");
                                                    }
                                                    if (split[1].trim().toUpperCase().equals("M")) {
                                                        MOTOR.setText("  MOTOR:  " + "手动模式");
                                                    }
                                                }
                                            });
                                            break;
                                        case "AST":
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    if (split[1].length() >= 1) {
                                                        String[] s1 = split[1].trim().split(" ");
                                                        for (String s2 : s1) {
                                                            strings.add(s2);
                                                        }
                                                    }
                                                }
                                            });
                                    }
                                }
                            }
                            Platform.runLater(new Runnable() {
                                @Override
                                public void run() {
                                    TMPLV.setItems(FXCollections.observableArrayList(items));
                                    PHLV.setItems(FXCollections.observableArrayList(strings));
                                }
                            });
                            flag = "";
                            flag1 = "";
                        }
                    };
                    text = Long.parseLong(NumLB.getText());
                    timer1.schedule(timerTask1, 0, text * 1000);
                } else {
                    timer1.cancel();
                }
            }
        });
        // Dash Board一栏刷新速率输入框的限制监听
        NumLB.setTextFormatter(new TextFormatter<Object>(change -> {
            if (!change.getText().isEmpty()) {
                return change.getText().matches("\\d+") && Double.parseDouble(change.getControlNewText()) > 0 && Double.parseDouble(change.getControlNewText()) <= 10 ? change : null;
            }
            return change;
        }));
        // Dash Board一栏刷新速率输入框的鼠标移出监听
        NumLB.setOnMouseExited(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                NumLB.setFocusTraversable(false);
                text = Long.parseLong(NumLB.getText());
                timer1.cancel();
                timer1 = new Timer();
                timerTask1 = new TimerTask() {
                    @Override
                    public void run() {
                        String flag = "";
                        String flag1 = "";
                        ArrayList<String> items = new ArrayList<>();
                        ArrayList<String> strings = new ArrayList<>();
                        for (String s : resultList) {
                            String[] split = s.split(":");
                            if (split.length > 1) {
                                switch (split[0].trim()) {
                                    case "PD 1":
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                System.out.println(split[0] + split[1]);
                                                PD1.setText("  PD1: " + split[1]);
                                            }
                                        });
                                        break;
                                    case "PD 2":
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                PD2.setText("  PD2: " + split[1]);
                                            }
                                        });
                                        break;
                                    case "PD 3":
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                PD3.setText("  PD3: " + split[1]);
                                            }
                                        });
                                        break;
                                    case "PD 4":
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                PD4.setText("  PD4: " + split[1]);
                                            }
                                        });
                                        break;
                                    case "PD 5":
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                PD5.setText("  PD5: " + split[1]);
                                            }
                                        });
                                        break;
                                    case "PD 6":
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                PD6.setText("  PD6: " + split[1]);
                                            }
                                        });
                                        break;
                                    case "HIDRV1":
                                    case "HIDEV1":
                                    case "HIDRV2":
                                    case "HIDEV2":
                                    case "PDBA":
                                    case "PUMP1":
                                    case "PUMP2":
                                    case "SP1":
                                    case "SP2":
                                    case "SP3":
                                    case "PSB":
                                    case "MAIN":
                                        items.add(split[0].trim() + " : " + split[1].trim());
                                        break;
                                    case "PUMP 1 ILD":
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                LD1.setText("  LD1:  " + split[1]);
                                            }
                                        });
                                        break;
                                    case "PUMP 2 ILD":
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                LD2.setText("  LD2:  " + split[1]);
                                            }
                                        });
                                        break;
                                    case "HIPUMP 1 ILD":
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                HP1.setText("  HP1:  " + split[1]);
                                            }
                                        });
                                        break;
                                    case "HIPUMP 2 ILD":
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                HP2.setText("  HP2:  " + split[1]);
                                            }
                                        });
                                        break;
                                    case "HIPUMP 3 ILD":
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                HP3.setText("  HP3:  " + split[1]);
                                            }
                                        });
                                        break;
                                    case "HIPUMP 4 ILD":
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                HP4.setText("  HP4:  " + split[1]);
                                            }
                                        });
                                        break;
                                    case "FAOM TRANS":
                                        if (flag == "") {
                                            flag = split[1].trim().toUpperCase();
                                        } else if (flag.toUpperCase().equals("OFF") && split[1].trim().toUpperCase().equals("OFF")) {
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    FAOM.setText("  FAOM:  " + "PWM");
                                                }
                                            });
                                        } else {
                                            String s1 = "";
                                            if (flag.equals("OFF") && split[1].trim().toUpperCase().equals("ON")) {
                                                s1 = "全通模式";
                                            } else if (flag.equals("ON") && split[1].trim().toUpperCase().equals("OFF")) {
                                                s1 = "关断模式";
                                            }
                                            String finalS = s1;
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    FAOM.setText("  FAOM:  " + finalS);
                                                }
                                            });

                                        }
                                        break;
                                    case "FAOM BLOCK":
                                        if (flag == "") {
                                            flag = split[1].trim().toUpperCase();
                                        } else if (flag.toUpperCase().equals("OFF") && split[1].trim().toUpperCase().equals("OFF")) {
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    FAOM.setText("  FAOM:  " + "PWM");
                                                }
                                            });
                                        } else {
                                            String s1 = "";
                                            if (flag.equals("OFF") && split[1].trim().toUpperCase().equals("ON")) {
                                                s1 = "关断模式";
                                            } else if (flag.equals("ON") && split[1].trim().toUpperCase().equals("OFF")) {
                                                s1 = "全通模式";
                                            }

                                            String finalS = s1;
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    FAOM.setText("  FAOM:  " + finalS);
                                                }
                                            });

                                        }
                                        break;
                                    case "SAOM TRANS":
                                        if (flag1 == "") {
                                            flag1 = split[1].trim().toUpperCase();
                                        } else if (flag1.equals(split[1].trim().toUpperCase())) {
                                            if (flag1.equals("OFF")) {
                                                Platform.runLater(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        FAOM.setText("  FAOM:  " + "PWM");
                                                    }
                                                });
                                            }
                                        } else {
                                            String s1 = "";
                                            if (flag1.equals("OFF") && split[1].trim().toUpperCase().equals("ON")) {
                                                s1 = "全通模式";
                                            } else if (flag1.equals("ON") && split[1].trim().toUpperCase().equals("OFF")) {
                                                s1 = "关断模式";
                                            }
                                            String finalS = s1;
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    SAOM.setText("  SAOM:  " + finalS);
                                                }
                                            });
                                        }
                                        break;
                                    case "SAOM BLOCK":
                                        if (flag1 == "") {
                                            flag1 = split[1].trim().toUpperCase();
                                        } else if (flag1.equals(split[1].trim().toUpperCase())) {
                                            if (flag1.equals("OFF")) {
                                                Platform.runLater(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        SAOM.setText("  SAOM:  " + "PWM");
                                                    }
                                                });
                                            }
                                        } else {
                                            String s1 = "";
                                            if (flag1.equals("OFF") && split[1].trim().toUpperCase().equals("ON")) {
                                                s1 = "关断模式";
                                            } else if (flag1.equals("ON") && split[1].trim().toUpperCase().equals("OFF")) {
                                                s1 = "全通模式";
                                            }
                                            String finalS = s1;
                                            Platform.runLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    SAOM.setText("  SAOM:  " + finalS);
                                                }
                                            });
                                        }
                                        break;
                                    case "MOTO MODE":
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                if (split[1].trim().toUpperCase().equals("A")) {
                                                    MOTOR.setText("  MOTOR:  " + "自动模式");
                                                }
                                                if (split[1].trim().toUpperCase().equals("M")) {
                                                    MOTOR.setText("  MOTOR:  " + "手动模式");
                                                }
                                            }
                                        });
                                        break;
                                    case "AST":
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                if (split[1].length() >= 1) {
                                                    String[] s1 = split[1].trim().split(" ");
                                                    for (String s2 : s1) {
                                                        strings.add(s2);
                                                    }
                                                }
                                            }
                                        });
                                }
                            }
                        }
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                TMPLV.setItems(FXCollections.observableArrayList(items));
                                PHLV.setItems(FXCollections.observableArrayList(strings));
                            }
                        });
                        flag = "";
                        flag1 = "";
                    }
                };
                timer1.schedule(timerTask1, text, text * 1000);
            }
        });
        // Dash Board一栏刷新速率输入框的鼠标进入监听
        NumLB.setOnMouseMoved(mouseEvent -> {
            NumLB.requestFocus();
        });
        // 单点刷新点击事件
        SingleLB.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                String flag = "";
                String flag1 = "";
                ArrayList<String> items = new ArrayList<>();
                ArrayList<String> strings = new ArrayList<>();
                for (String s : resultList) {
                    String[] split = s.split(":");
                    if (split.length > 1) {
                        switch (split[0].trim()) {
                            case "PD 1":
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        System.out.println(split[0] + split[1]);
                                        PD1.setText("  PD1: " + split[1]);
                                    }
                                });
                                break;
                            case "PD 2":
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        PD2.setText("  PD2: " + split[1]);
                                    }
                                });
                                break;
                            case "PD 3":
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        PD3.setText("  PD3: " + split[1]);
                                    }
                                });
                                break;
                            case "PD 4":
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        PD4.setText("  PD4: " + split[1]);
                                    }
                                });
                                break;
                            case "PD 5":
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        PD5.setText("  PD5: " + split[1]);
                                    }
                                });
                                break;
                            case "PD 6":
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        PD6.setText("  PD6: " + split[1]);
                                    }
                                });
                                break;
                            case "HIDRV1":
                            case "HIDEV1":
                            case "HIDRV2":
                            case "HIDEV2":
                            case "PDBA":
                            case "PUMP1":
                            case "PUMP2":
                            case "SP1":
                            case "SP2":
                            case "SP3":
                            case "PSB":
                            case "MAIN":
                                items.add(split[0].trim() + " : " + split[1].trim());
                                break;
                            case "PUMP 1 ILD":
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        LD1.setText("  LD1:  " + split[1]);
                                    }
                                });
                                break;
                            case "PUMP 2 ILD":
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        LD2.setText("  LD2:  " + split[1]);
                                    }
                                });
                                break;
                            case "HIPUMP 1 ILD":
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        HP1.setText("  HP1:  " + split[1]);
                                    }
                                });
                                break;
                            case "HIPUMP 2 ILD":
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        HP2.setText("  HP2:  " + split[1]);
                                    }
                                });
                                break;
                            case "HIPUMP 3 ILD":
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        HP3.setText("  HP3:  " + split[1]);
                                    }
                                });
                                break;
                            case "HIPUMP 4 ILD":
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        HP4.setText("  HP4:  " + split[1]);
                                    }
                                });
                                break;
                            case "FAOM TRANS":
                                if (flag == "") {
                                    flag = split[1].trim().toUpperCase();
                                } else if (flag.toUpperCase().equals("OFF") && split[1].trim().toUpperCase().equals("OFF")) {
                                    Platform.runLater(new Runnable() {
                                        @Override
                                        public void run() {
                                            FAOM.setText("  FAOM:  " + "PWM");
                                        }
                                    });
                                } else {
                                    String s1 = "";
                                    if (flag.equals("OFF") && split[1].trim().toUpperCase().equals("ON")) {
                                        s1 = "全通模式";
                                    } else if (flag.equals("ON") && split[1].trim().toUpperCase().equals("OFF")) {
                                        s1 = "关断模式";
                                    }
                                    String finalS = s1;
                                    Platform.runLater(new Runnable() {
                                        @Override
                                        public void run() {
                                            FAOM.setText("  FAOM:  " + finalS);
                                        }
                                    });

                                }
                                break;
                            case "FAOM BLOCK":
                                if (flag == "") {
                                    flag = split[1].trim().toUpperCase();
                                } else if (flag.toUpperCase().equals("OFF") && split[1].trim().toUpperCase().equals("OFF")) {
                                    Platform.runLater(new Runnable() {
                                        @Override
                                        public void run() {
                                            FAOM.setText("  FAOM:  " + "PWM");
                                        }
                                    });
                                } else {
                                    String s1 = "";
                                    if (flag.equals("OFF") && split[1].trim().toUpperCase().equals("ON")) {
                                        s1 = "关断模式";
                                    } else if (flag.equals("ON") && split[1].trim().toUpperCase().equals("OFF")) {
                                        s1 = "全通模式";
                                    }

                                    String finalS = s1;
                                    Platform.runLater(new Runnable() {
                                        @Override
                                        public void run() {
                                            FAOM.setText("  FAOM:  " + finalS);
                                        }
                                    });

                                }
                                break;
                            case "SAOM TRANS":
                                if (flag1 == "") {
                                    flag1 = split[1].trim().toUpperCase();
                                } else if (flag1.equals(split[1].trim().toUpperCase())) {
                                    if (flag1.equals("OFF")) {
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                FAOM.setText("  FAOM:  " + "PWM");
                                            }
                                        });
                                    }
                                } else {
                                    String s1 = "";
                                    if (flag1.equals("OFF") && split[1].trim().toUpperCase().equals("ON")) {
                                        s1 = "全通模式";
                                    } else if (flag1.equals("ON") && split[1].trim().toUpperCase().equals("OFF")) {
                                        s1 = "关断模式";
                                    }
                                    String finalS = s1;
                                    Platform.runLater(new Runnable() {
                                        @Override
                                        public void run() {
                                            SAOM.setText("  SAOM:  " + finalS);
                                        }
                                    });
                                }
                                break;
                            case "SAOM BLOCK":
                                if (flag1 == "") {
                                    flag1 = split[1].trim().toUpperCase();
                                } else if (flag1.equals(split[1].trim().toUpperCase())) {
                                    if (flag1.equals("OFF")) {
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                SAOM.setText("  SAOM:  " + "PWM");
                                            }
                                        });
                                    }
                                } else {
                                    String s1 = "";
                                    if (flag1.equals("OFF") && split[1].trim().toUpperCase().equals("ON")) {
                                        s1 = "关断模式";
                                    } else if (flag1.equals("ON") && split[1].trim().toUpperCase().equals("OFF")) {
                                        s1 = "全通模式";
                                    }
                                    String finalS = s1;
                                    Platform.runLater(new Runnable() {
                                        @Override
                                        public void run() {
                                            SAOM.setText("  SAOM:  " + finalS);
                                        }
                                    });
                                }
                                break;
                            case "MOTO MODE":
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (split[1].trim().toUpperCase().equals("A")) {
                                            MOTOR.setText("  MOTOR:  " + "自动模式");
                                        }
                                        if (split[1].trim().toUpperCase().equals("M")) {
                                            MOTOR.setText("  MOTOR:  " + "手动模式");
                                        }
                                    }
                                });
                                break;
                            case "AST":
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (split[1].length() >= 1) {
                                            String[] s1 = split[1].trim().split(" ");
                                            for (String s2 : s1) {
                                                strings.add(s2);
                                            }
                                        }
                                    }
                                });
                        }
                    }
                }
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        TMPLV.setItems(FXCollections.observableArrayList(items));
                        PHLV.setItems(FXCollections.observableArrayList(strings));
                    }
                });
                flag = "";
                flag1 = "";
            }
        });
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
        String name = "log" + dateFormat.format(new Date()) + ".txt";
        String path = System.getProperty("user.dir") + "/log/" + name;
        timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                if (port != null && port.isOpen() && resultList.size() > 0 && resultList.get(1) != "") {
                    for (String s : resultList) {
                        String[] split = s.split(":");
                        switch (split[0]) {
                            case "LASER PSP":
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        //更新JavaFX的主线程的代码放在此处
                                        double v = Double.parseDouble(split[1].trim());
                                        LB1.setText(String.format("%.2f", v) + " W");
                                    }
                                });
                                break;
                            case "LASER FRQ":
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        String[] s1 = split[1].trim().split(" ");
                                        double v = Double.parseDouble(s1[0].trim());
                                        LB2.setText(String.format("%.2f", v / 1000) + "KHz");
                                    }
                                });
                                break;
                            case "LASER EN":
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (split[1].trim().toUpperCase().equals("OFF")) {
                                            stopBT.setText("GO");
                                        } else {
                                            stopBT.setText("STOP");
                                        }
                                    }
                                });
                                break;
                        }
                    }
                    try {
                        File file = new File(path);
                        if (!file.exists() && !file.isDirectory()) {
                            file.getParentFile().mkdirs();
                        }
                        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, true));
                        bufferedWriter.append(OPPLB.getText() + ":" + LB1.getText() + "\t");
                        bufferedWriter.append(PRRLB.getText() + ":" + LB2.getText() + "\t");
                        Date date = new Date();
                        SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        bufferedWriter.append(dataFormat.format(date) + "\n");
                        bufferedWriter.flush();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
        timer.schedule(timerTask, 0, 1000);
        // 显示日志
        LogLB.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (port != null && port.isOpen()) {
                    if (stage3 != null) {
                        stage3.close();
                    }
                    stage3 = new Stage();
                    GridPane pane = new GridPane();
                    ListView listView = new ListView();
                    File file = new File(path);
                    if (!file.exists() && !file.isDirectory()) {
                        file.getParentFile().mkdirs();
                    }
                    ArrayList<String> strings = new ArrayList<>();
                    try {
                        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                        String s = null;
                        while ((s = reader.readLine()) != null) {
                            strings.add(s);
                        }
                    } catch (FileNotFoundException e) {
                        throw new RuntimeException(e);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    listView.setItems(FXCollections.observableArrayList(strings));
                    listView.setPrefWidth(500);
                    pane.add(listView, 0, 0);
                    Scene scene1 = new Scene(pane);
                    stage3.setScene(scene1);
                    stage3.setResizable(false);
                    stage3.show();
                }
            }
        });

        // 窗口关闭监听，关闭定时任务
        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent windowEvent) {
                if (timer1 != null) {
                    timer1.cancel();
                }
                if (port != null && port.isOpen()) {
                    port.closePort();
                }
                if (timer2 != null) {
                    timer2.cancel();
                }
                if (stage1 != null) {
                    stage1.close();
                }
                if (stage2 != null) {
                    stage2.close();
                }
                if (stage3 != null) {
                    stage3.close();
                }
                if (stage4 != null) {
                    stage4.close();
                }
                if (newstage != null) {
                    newstage.close();
                }
                if (timer != null) {
                    timer.cancel();
                }
            }
        });
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}